How to write a Python 3 script to do .\activate in Windows 10 OS? - windows

I'm trying to write a script to activate the virtual environment under a path, which is doing the same thing as the following command line:
cd C:\go\to\my\venv\path\Scripts
.\activate
My idea is first to change the directory to my Scripts folder, then execute the .\activate file. The problem is I have tried
1. os.system(".\\activate") #".\activate" is an error
2. subprocess.run(".\\activate")
3. subprocess.call(".\\activate")
4. subprocess.Popen(".\\activate", shell=True)
but all failed, and because I'm new to Python 3, so any suggestion will be appreciated, thanks.
Update 1:
Right now, it seems like can install one package successfully, but I have a list of packages need to install and I will figure it out how to do it. When I print out os.getcwd() after I activated the venv, there is no (venv) as the prefix in the result, so I don't know if the package was installed under venv or not?
Update 2:
Now, my problem is I'm using from pip._internal import main as pip
pip(['install', package_name])
It only can install one package successfully, if I pass a list of packages and use for loop to install them then they will fail. The error will be
Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\Users\BO7D71~1.CHE\AppData\Local\Temp\pip-req-tracker-1nc0ofuc\5fc2bc8999692b42fb54fd13e7cbdf5f855b9718f5363d30a270bbe1'
Update 3:
Problem solved!

Related

Can't import on Windows/Anaconda

Windows 10, Anaconda 2 or 3
c:\leo.repo\leo-editor>c:\apps\Git\bin\git.exe --version
yields:
git version 2.8.2.windows.1
sys.path contains C:\apps\Git, C:\apps\Git\bin and C:\apps\Git\cmd, but I am getting the dreaded GitCommandNotFound exception. What am I doing wrong?
Executing:
from git import Repo
yields:
[snip]
File "c:\Anaconda3\lib\site-packages\git\cmd.py", line 602, in execute
raise GitCommandNotFound(command, err)
git.exc.GitCommandNotFound: Cmd('git') not found due to: FileNotFoundError('[WinError 2] The system cannot find the file specified')
cmdline: git version
Edward
It appears that installs are different on Anaconda.
Per this page the proper installation procedure is:
conda install -c conda-forge gitpython=2.1.1
Installation on Python 2 was straightforward.
Update: I had to add the following to sys.path for Python 3:
C:\Anaconda3\pkgs\gitdb-0.6.4-py35_1\Lib\site-packages
I have no idea why python 2 installs in site-packages as expected, while python 3 installs in the path shown above.
Update2: I also had to update the Windows PATH variable (not just sys.path) to point to the directory containing git.exe.

Python modules not working in IDLE

Working on Automate The Boring Stuff project and having trouble with pyperclip and IDLE. pyperclip is successfully downloaded and works fine in Terminal but when I import pyperclip in IDLE, I get the below error.
UPDATE - This is happening with other modules - anything I've installed in the terminal aren't importing into IDLE.
import pyperclip
Traceback (most recent call last):
File "", line 1, in
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
I figure I have the module saved in the wrong folder somehow. Below are the sys.path outputs from my Terminal and IDLE.
In terminal:
'/anaconda/bin',
'//anaconda/lib/python36.zip',
'//anaconda/lib/python3.6',
'//anaconda/lib/python3.6/lib-dynload',
'//anaconda/lib/python3.6/site-packages',
'//anaconda/lib/python3.6/site-packages/Sphinx-1.5.1-py3.6.egg',
'//anaconda/lib/python3.6/site-packages/aeosa',
'//anaconda/lib/python3.6/site-packages/IPython/extensions',
'/Users/andrewricardo/.ipython']
In IDLE:
sys.path
['', '/Users/andrewricardo/Documents', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
There are multiple paths here - how do I effectively move the pyperclip module to the correct place?
OR - Is there something else going on here?
Each python installation and uses its own .../lib/site-packages for 3rd party modules, and you have two. If you ran the non-anaconda python in the terminal, the same that is running IDLE, it would have the same problem. Indeed, that python is the source of the ImportErrors.
Option 1: Separately install all packages you want to use with the non-anaconda python (and IDLE) in its own site-package directory. In IDLE Shell, get the path to its executable
>>> import sys; sys.executable
Save are remember the resulting 'python-path'. Then in the terminal, run
python-path -m pip install package-name
for each package.
Option 2 (uses a little known feature of site-package directories): in
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
add a file name, for instance, anaconda.pth, containing one line.
//anaconda/lib/python3.6/site-packages
(I have no idea why the double / or if it is really needed; I just copied it from your question.) This makes the anaconda python site-packages an extension of the framework python site-packages. Since both are for 3.6, one copy should work for both.
Had this same issue and googled
'why is importing pyperclip in my python file giving an "modulenotfounderror"but working fine in the idle'
and this page came up. then i also checked this [installed Pyperclip, trouble importing to IDLE.
I am new to python, had the same issue using the same book after pip installing pyperclip here's what I did to solve this issue.
I copied the pyperclip-1.7.0-py3.7.egg-info and pyperclip folders from
C:\Users\USER\AppData\Local\Programs\Python\Python37\Lib\site-packages into the C:\Users\USER\Anaconda3\Lib\site-packages folders and C:\Users\USER\AppData\Local\Programs\Python\Python37 folders and it worked. Hope this helps. If it doesn't. Try this code in your text editor because that's what I did using Atom and how i was able to solve it.
import sys
print(sys.executable)
Then copy the pyperclip-1.7.0-py3.7.egg-info and pyperclip folders into the path that the above code gives you.

Why can't pip find pysvn?

I'm working on a project which was written in Python 2, and I'm upgrading it to Python 3. So far, I've just been finding minor syntax errors which are easily fixable. What I've done is created a new project in Python 3, ensured that it worked, and copies chunks of code from the old project into the new one.
Right now, I'm having trouble with pysvn. Initially, I was getting this error:
ImportError: No module named 'pysvn'
At this point, I tried using pip install pysvn, which didn't work. I got the following:
pip install pysvn
Collecting pysvn
Could not find a version that satisfies the requirement pysvn (from versions:)
No matching distribution found for pysvn
So then after a bit of research, I went to the pysvn download site and tried:
>pip install --index-url http://pysvn.tigris.org/project_downloads.html pysvn, which gave me this error:
Collecting pysvn
The repository located at pysvn.tigris.org is not a trusted or secure host and is being ignored. If this repository is available via HTTPS it is recommended to use HTTPS instead, otherwise you may silence this warning and allow it anyways with '--trusted-host pysvn.tigris.org'.
and also the same error as when I tried >pip install pysvn.
My next step was to manually download the .exe file for the version I needed, and I was able to successfully install pysvn. I have checked the site-packages directory, and pysvn is indeed there, but pip still can't tell me anything about it:
>pip show pysvn
>
When I do this for another installed module, selenium for example, I get the following:
pip show selenium
Metadata-Version: 1.1
Name: selenium
Version: 2.49.2
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Location: ...\lib\site-packages
Requires:
I was able to verify that the installation of pysvn was successful because my project now runs instead of giving me that ImportError.
So why can pip not give me information for another module in the same directory that was successfully installed?
As it turns out, because I didn't use pip install for pysvn, pip didn't know that pysvn existed. Because it wasn't available from PyPI (the Python Package Index), there was no way that pip could see it (because that's where pip goes first to find packages that it's attempting to install).
From the pip user guide:
pip supports installing from PyPI, version control, local projects, and directly from distribution files.
Since I had eventually downloaded pysvn from its own download site (which was not any of the above 4 options) and ran the .exe manually, pip simply doesn't know about it even though it's in the same directory as other packages installed by pip.
I suppose I could've also retrieved the distribution files and used pip with those, but my workaround did the trick.
My way on linux:
Get sources from here
tar -zxf pysvn-1.9.10.tar.gz
apt-get install subversion libsvn1 libsvn-dev make g++
cd pysvn-1.9.10/Source
python setup.py configure --pycxx-dir=/pysvn-1.9.10/Import/pycxx-7.1.3/
make
Here i've got errors below:
Compile: /pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxxsupport.cxx into cxxsupport.o
/pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxxsupport.cxx:42:10: fatal error: Src/Python3/cxxsupport.cxx: No such file or directory
#include "Src/Python3/cxxsupport.cxx"
Compile: /pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxxextensions.c into cxxextensions.o
/pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxxextensions.c:42:10: fatal error: Src/Python3/cxxextensions.c: No such file or directory
#include "Src/Python3/cxxextensions.c"
It is needed to edit that files:
vi /pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxxsupport.cxx
change #include "Src/Python3/cxxsupport.cxx" to
#include "Python3/cxxsupport.cxx"
and same on second file. Than make again:
make clean && make
...
Compile: /code/pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxxextensions.c into cxxextensions.o
Compile: /code/pysvn-1.9.10/Import/pycxx-7.1.3/Src/IndirectPythonInterface.cxx into IndirectPythonInterface.o
Compile: /code/pysvn-1.9.10/Import/pycxx-7.1.3/Src/cxx_exceptions.cxx into cxx_exceptions.o
Link pysvn/_pysvn_3_7.so
Then just copy it to the site-packages (change to yours directory):
mkdir /usr/local/lib/python3.7/site-packages/pysvn
cp /code/pysvn-1.9.10/Sources/pysvn/__init__.py /usr/local/lib/python3.7/site-packages/
cp /code/pysvn-1.9.10/Sources/pysvn/_pysvn*.so /usr/local/lib/python3.7/site-packages/

pip install --user error under OS X (virtualenv doesn't fix it)

I used to install python (2.7.6) packages using sudo pip install [...] until I recently decided that this was a bad idea and I should use: pip install --user [...] instead.
Since I am using Mac OS X (10.10.3), my python modules had been installed into /Library/Python/2.7/site-packages. Now, with the --user option enabled, they are being installed into: /Users/USER/Library/Python/2.7/lib/python/site-packages
I have since moved all packages (including pip/setuptools) into this destination, adjusted my $PYTHONPATH and changed the file ownership to my local user. I expected that I would now be able to call pip install --user [..] and everything should go smoothly.
Unfortunately, this is not the case. For any pip install (or pip install --upgrade) command I receive the same error (below). The same is true when using a virtualenv - the same error shows up and I have no clue why.
However, everything does work "fine", when I use sudo pip install --user, which is why I assume that it is somehow related to a file-permission error I can not see. (It's not exactly fine, as the newly installed package will be in the right location, but all files are owned by root.)
Any idea what I am doing wrong or how to debug this?
pip install --upgrade --user tornado
Collecting tornado
Using cached tornado-4.1.tar.gz
Exception:
Traceback (most recent call last):
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/basecommand.py", line 223, in main
status = self.run(options, args)
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/commands/install.py", line 280, in run
requirement_set.prepare_files(finder)
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/req/req_set.py", line 317, in prepare_files
functools.partial(self._prepare_file, finder))
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/req/req_set.py", line 304, in _walk_req_to_install
more_reqs = handler(req_to_install)
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/req/req_set.py", line 483, in _prepare_file
abstract_dist.prep_for_dist()
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/req/req_set.py", line 123, in prep_for_dist
self.req_to_install.run_egg_info()
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/req/req_install.py", line 368, in run_egg_info
self.setup_py, self.name,
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/pip/req/req_install.py", line 339, in setup_py
import setuptools # noqa
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/setuptools/__init__.py", line 11, in <module>
from setuptools.extension import Extension
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/setuptools/extension.py", line 8, in <module>
from .dist import _get_unpatched
File "/Users/USER/Library/Python/2.7/lib/python/site-packages/setuptools/dist.py", line 21, in <module>
packaging = pkg_resources.packaging
AttributeError: 'module' object has no attribute 'packaging'
I also checked the location of pkg_resources and deleted pkg_resources.pyc, which is located in /usr/local/lib/python2.7/site-packages for some reason, but it didn't change anything. (My local user also has full access rights in this location.)
[UPDATE:]
For some reason I was able to install virtualenv without root (pip install --user virtualenv), but all other packages I tried still fail. Also, if I switch to a new virtual environment, I still receive the same error. I am now thinking this is related to my $PYTHONPATH variable, which appears to not be updated for my virtualenv, but I haven't found the real solution, yet.
Alright, so I found the actual problem and solution. My $PYTHONPATH contained an invalid directory. I setup my $PYTHONPATH a long time ago and wrote it into a .bashrc (or more accurately .bash_profile) script.
pip was probably looking through all directories in $PYTHONPATH, although I don't know what it is doing in each of them that causes the above error, but removing the invalid directory fixed my problems.
I also now highly recommend to just use virtualenv instead of a static setup.

kmpfit module not in Kapteyn v2.2 module list

I am using Python 2.7 through Anaconda 2.7.8 and need Kapteyn 2.2 to perform Non-linear Least Squares fitting easily (it is probably an alternative to Scipy.optimize.leastsq() for dummies like me!).
After copy-pasting this from a previous post here on Stack Overflow:
conda install -c https://conda.binstar.org/dhirschfeld pyodbc
and then running on my cmd (as I did not have pyodbc installed I think, because of which maybe the command prompt on my Windows 7 64-bit system was not responding well to python setup.py install inside the Anaconda directory where I unzipped the Kapteyn .zip file downloaded from University of Groningen website.
But, after the installing pyodbc properly and running python setup.py install, the cmd gave me an error saying error: command 'C:\Users\windows 7\Anaconda\Scripts\gcc.bat' failed with exit status 1. Later, when I tried to import kmpfit module (needed for Non-linear least square fitting with Kapteyn), here is the problem:
import kapteyn
help(kapteyn)
Help on package kapteyn:
NAME
kapteyn - Kapteyn package.
FILE
c:\users\windows 7\anaconda\kapteyn\__init__.py
PACKAGE CONTENTS
_ni_support
celestial
doccer
filters
interpolation
maputils
mplutil
positions
rulers
shapes
tabarray
wcsgrat
DATA
__all__ = ['celestial', 'wcs', 'wcsgrat', 'tabarray', 'maputils', 'mpl...
__version__ = '2.2'
VERSION
2.2
As you can see, there is no module named kmpfit (or even wcs) here. But according to http://www.astro.rug.nl/software/kapteyn/intro.html, these two should be there.
Kindly help. I have never imported any module before.
Thanks in advance...:-)
I just managed to get this working (on Mac OSX, so you may have to adjust this). My steps were:
$ conda install pyodbc (didn't need to go through binstar)
Download & unarchive the kapteyn package, then navigate to its directory
$ python setup.py install, which used my OS's C compiler and Anaconda's python, and installed kapteyn to my anaconda distro's site-packages, as it should.
Check that kmpfit.so is in the kapteyn folder in site-packages, showing that kmpfit installed correctly.
>> from kapteyn import kmpfit failed, ImportError: cannot import name kmpfit. I did some digging and discovered that it was still importing kapteyn from the folder that I downloaded, not from site-packages.
Delete the downloaded kapteyn folder, then try again. It worked!

Resources