OK, I loaded Python 3.5 PYVISA, and set up my path variables in Windows, and import visa worked! Yay!
Now I want to create an executable, using cx_Freeze. I try running it, and it says it wants to see Python 3.4.
OK, I load Python 3.4. I add python34 paths to Windows and start up idle, load my script, and try to import visa, no module named visa.
What do I have to do to get the script to run under Python 3.4?
thank you
If you have installed pyvisa by using below command:
pip install pyvisa
Then it gets installed in site packages of python 3.5 (or) site packages of that specific python version (whichever python that is serving at that point of time)
If you want that to be available for all python versions, install it to separate folder and add that path as value to PYTHONPATH (system variable). If there is no such variable, create one. Command to install it to separate folder is:
pip install pyvisa -t c:\external
(Or)
Re-install it using pip from specific python version
C:\Python34\python.exe -m pip install pyvisa
Related
OS: windows 10
I install python3.9, pip install many 3rd party packages and use them well on both Pycharm and CMD terminal;
Later, I install MSYS2 and then I tpye 'python' on the terminal of MSYS2 and get into python terminal; it seems well until now. When I import sth, getting the warning "No module named 'xxx'" which can be imported well on Pycharm or CMD terminal.
so I guess the path of 3rd party packages not be included for MSYS2, how to resovle it?
Here is the standard way to use Python 3 and pip in MSYS2:
Select the "MSYS2 MinGW 64-bit" shortcut or run mingw64.exe to start a MinGW 64-bit shell. (32-bit should work too.)
Run pacman -S $MINGW_PACKAGE_PREFIX-{python3,python3-pip} to install both Python 3 and pip.
Run pip install PKGNAME to install a package you need.
Run python path/to/script.py to run your script.
I tested these instructions just now. I was able to run pip install pyserial and then I was able to run a script that started with import serial.
I followed along with the package distribution tutorial for uploading a package on test.pypi.org from a Linux machine, and proceeded to try to install the package on a separate Windows machine.
On the Windows machine, I installed Python 3.8 and ticked the Add Python 3.8 to PATH box. No previous Python installation was present, and it installed to C:\Users\me\AppData\Local\Programs\Python\Python38\.
In Environmental Variables, my PATH under "Users variables" is: C:\Users\me\AppData\Local\Programs\Python38\Scripts\;C:\Users\me\AppData\Local\Programs\Python38\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
My PATH under "System variables" is much longer, but it contains no Python directories.
I then downloaded and installed pip.
In Command Prompt, I first created a virtual environment with py -m venv tutorial and activated it with Scripts\activate. Activation was verified by the (tutorial) next to the prompt. Then, in the virtual env, with py -m pip install ... I installed my example package.
I can verify it's installed by seeing it in the output of py -m pip list:
Package Version
----------------------- -------
example-pkg-me 0.0.1
pip 19.2.3
setuptools 41.2.0
example-pkg-me does not appear outside the virtual env.
The tutorial finishes by having you attempt an import in the Python interpreter:
py
>>> import example_pkg
but I get ModuleNotFoundError: No module named `example_pkg`. Why? Is something wrong with either of my PATHs? Note that the tutorial specifically mentions omitting the username (and import example_pkg_me issues the same error regardless).
Curiously, when trying to install and import a "mainstream (?)" package, it succeeds with no errors. I.e.,
py -m pip install numpy
and
py
>>> import numpy
works.
Update:
Same results on a separate Linux machine with the same steps where the only differences are that Python and pip were already present.
As an aside, if your package is intended to be run (as a GUI app) rather than imported in the interpreter as in the tutorial, how would the person who installed it run it? Would they have to navigate to the directory where the package files were physically installed and run a main.py script there? Is there a better way (like freezing the package as an executable with the likes of cx_Freeze and distributing the EXE instead of relying on pip installations)? Or maybe just expect them to download a repository, run py setup.py install, and then a main.py script?
I try to install Theano by Anaconda. It works, but when I enter the python -i, import theano shows No module named 'theano'. Do I need to switch another interpreter of Python, how? Also, for the packages installed by conda, if I don't double install them, can I find in Python? How is Python related to Python by Anaconda? Thanks!!!
I had have a similar issue, trying to install folium. If you are using the Anaconda:
When you install using conda install -c conda-forge folium, the package will be placed in:
./anaconda3/envs/[name env]/lib/python3.7/site-packages/folium
When you install using pip (with a anaconda env activated), pip install folium, the package will be placed in:
./anaconda3/lib/python3.7/site-packages/folium
Python use first the sites-packages as the target directory of manually built python packages. When you build and install python packages from source (using distutils, probably by executing python setup.py install ), you will find the installed modules in site-packages by default.
In this case you have two places: /anaconda3/lib/python3.7/site-packages/ and /anaconda3/envs/[name env]/lib/python3.7/site-packages/.
First the modules will be available as default in /anaconda3/lib/python3.7/site-packages/. Sometimes (and I really don't know why) the modules inside sites-packages conda env are not available to import automatically without export the PATH.
So, to solve this issue, you have 2 options:
Installing using pip install folium and import folium (don't need install by conda install), or
After conda install , run conda init, close the terminal and open a new one. So, try to import again.
Here are some tips about use a pip in a conda-environment.
You can refer to a specific version of python by using the following at the first line of your .py file
This is for python 2.7
#!/usr/bin/env python2.7
This is for python 3
#!/usr/bin/env python3
As other users already pointed out you need to check if your module is included in your sys path. Use code:
import sys
print(sys.path)
If not you can include this in your sys.path by using the command:
sys.path.append('/path/to/the/folder/of/your/module/file')
or place it in default PYTHONPATH itself.
Other great answers:
https://stackoverflow.com/a/19305076/5381704
The problem is that in the code editor you are using, you are running the default interpreter. Based on your code editor, change the python interpreter to the conda interpreter and it will work.
In my case that happened because conda screwed up the environment variables. Instead of using env-specific python and pip, it used the globally installed ones.
Solution:
conda deactivate your-env
conda activate your-env
In my workstation, I was able to solve No module named <module name> error using two different ways.
First method, I solved this temporarily by:
(1) Open a Terminal
(2) $ conda activate <Conda environment name>
(3) $ export PYTHONPATH=/home/<user name>/anaconda3/envs/<Conda environment name>/lib/<Python package version>/site-packages:$PYTHONPATH
It is a temporary solution. Whenever you run your virtual environment, you have to do this.
My runtime environment:
OS: Unbuntu 18.04
Conda version: 4.8.2
Conda-build version: 3.18,11
Python version 3.7.6.final.0
Second method, I removed the
alias python=/usr/bin/python3.6 line in bashrc file.
Somehow this line blocks using Python tools installed in Anaconda Virtual Environment if the Python version in the Virtual Environment is different.
I am running Windows x64 bit.
I downloaded the Pyro4 package via pip install Pyro4. It downloaded the packages successfully and they are all present in my "C:\Python34\Scripts" folder as I've kept Python3.4 as default.
Now when I went to the that "C:\Python27\Scripts" folder, the Pyro4 package is not to be found. This is as expected, but I would like to work on both Python 2.7 and 3.4 as Pyro4 is compatible in both.
How do I change my pip command to download the package to Python 2.7's installation scripts directory?
First make a new environment variable:
Go to your system properties
Under Advanced tab click Environment Variables...
Under System variables section click New...
Variable name: (whatever you can remember for example p27s)
Variable value: your python 2.7 scripts folder ("C:\Python27\Scripts\")
From now on whenever you want to install a package for python 2.7 you can do it this way: %your_variable_name%pip install package_name
For example: C:>%p27s%pip install Pyro4
This way you can install any package for python 2.7 and use default pip for python 3.4
You will need to go to your environment variables in the control panel and change the path from C:\Python34\Scripts to C:\Python27\Scripts. After that change, when you type 'python' in the command prompt it will be using Python 2.7. Next, install pip like you initially did.
Not exactly what you're asking, but you could check out Anaconda which allows you switch python environments really easily.
https://www.continuum.io/downloads
Edit
Just to clarify
if you were to use this method, you can have named environments say python27 and python34
then you can just:
activate python27
pip install module
activate python34
pip install module
and it will install them to both environments.
I'd like to make a switch from Windows to Linux (Ubuntu) writing my python programs but I just can't get things to work. Here's the problem: I can see that there are quite the number of modules pre-installed (like numpy, pandas, matplotlib, etc.) in Ubuntu. They sit nicely in the /host/Python27/Lib/site-packages directory. But when I write a test python script and try to execute it, it gives me an ImportError whenever I try to import a module (for instance import numpy as np gives me ImportError: No module named numpy). When I type which python in the commandline I get the /usr/bin/python path. I think I might need to change things related to the python path, but I don't know how to do that.
You can use the following command in your terminal to see what folders are in your PYTHONPATH.
python -c "import sys, pprint; pprint.pprint(sys.path)"
I'm guessing /host/Python27/Lib/site-packages wont be in there (it doesn't sound like a normal python path. How did you install these packages?).
If you want to add folders to your PYTHONPATH then use the following:
export PYTHONPATH=$PYTHONPATH:/host/Python27/Lib/site-packages
Personally here are some recommendations for developing with Python:
Use virtualenv. It is a very powerful tool that creates sandboxed python environments so you can install modules and keep them separate from the main interpreter.
Use pip - When you've created a virtualenv, and activated it you can use pip install to install packages for you. e.g. pip install numpy will install numpy into your virtual environment and will be accessible from only this virtualenv. This means you can also install different versions for testing etc. Very powerful. I would recommend using pip to install your python packages over using ubuntu apt-get install as you are more likely to get the newer versions of modules (apt-get relies on someone packaging the latest versions of your python libraries and may not be available for as many libraries as pip).
When writing python scripts that you will make executable (chmod +x my_python_script.py) make sure you put #!/usr/bin/env python at the top as this will pick up the python interpreter in your virtual environment. If you don't (and put #!/usr/bin/python) then running ./my_python_script.py will always use the system python interpreter.
/host/Python27/Lib/site-packages is not a default python directory on linux installations as far as I am aware.
The normal python installation (and python packages) should be found under /usr/lib or /usr/lib64 depending on your processor architecture.
If you want to check where python is searching in addition to these directories you can use a terminal with the following command:
echo $PYTHONPATH
If the /host/Python27/Lib/site-packages path is not listed, attempt to use the following command and try it again:
export PYTHONPATH=$PYTHONPATH:host/Python27/Lib/site-packages
If this should work and you do not want to write this in a terminal every time you want to use these packages, simply put it into a file called .bashrc in your home folder (normally /home/<username>).
When installing other python libraries, specify the pip version you want to install it to, if it's python2 you use, then enter this syntax:
pip2 install <package>
For python3
pip3 install <package>